package com.als.config;

import lombok.Data;

import java.util.Properties;

@Data
public class DatabaseConfig {
    private String url;
    private String username;
    private String password;
    private String driver;
    private String query;
    private ConnectionPool connectionPool;
    private SparkJdbcConfig sparkJdbc;

    /**
     * 将配置转换为 Properties
     */
    public Properties toProperties() {
        Properties props = new Properties();
        props.setProperty("user", this.username);
        props.setProperty("password", this.password);
        props.setProperty("driver", this.driver);

        // 添加 Spark JDBC 优化参数
        if (sparkJdbc != null) {
            if (sparkJdbc.getFetchSize() != null) {
                props.setProperty("fetchSize", sparkJdbc.getFetchSize().toString());
            }
            if (sparkJdbc.getPartitionColumn() != null) {
                props.setProperty("partitionColumn", sparkJdbc.getPartitionColumn());
            }
            if (sparkJdbc.getLowerBound() != null) {
                props.setProperty("lowerBound", sparkJdbc.getLowerBound());
            }
            if (sparkJdbc.getUpperBound() != null) {
                props.setProperty("upperBound", sparkJdbc.getUpperBound());
            }
            if (sparkJdbc.getNumPartitions() != null) {
                props.setProperty("numPartitions", sparkJdbc.getNumPartitions().toString());
            }
        }

        return props;
    }

    @Data
    public static class ConnectionPool {
        private Integer initialSize;
        private Integer maxActive;
        private Integer minIdle;
    }

    @Data
    public static class SparkJdbcConfig {
        private Integer fetchSize;
        private String partitionColumn;
        private String lowerBound;
        private String upperBound;
        private Integer numPartitions;
    }
}
